﻿using NPOI.SS.UserModel;
using System;
using System.Runtime.CompilerServices;

public static class ExcelExtension
{
    public static string ToCsvString(this ISheet sheet)
    {
        string str = string.Empty;
        IRow firstRow = sheet.GetRow(0);
        for (int i = 0; i <= sheet.LastRowNum; i++)
        {
            IRow row = sheet.GetRow(i);
            string str2 = ToCsvString(firstRow, row);
            if (str2 != string.Empty)
            {
                str = str + str2 + Environment.NewLine;
            }
        }
        return str;
    }

    private static string ToCsvString(IRow firstRow, IRow row)
    {
        if (row == null)
        {
            return string.Empty;
        }
        string str = string.Empty;
        short lastCellNum = firstRow.LastCellNum;
        for (int i = 0; i < lastCellNum; i++)
        {
            ICell cell = row.GetCell(i);
            str = str + ToString(cell);
            if (i < (lastCellNum - 1))
            {
                str = str + ",";
            }
        }
        return str;
    }

    private static string ToString(CellValue cell)
    {
        if (cell == null)
        {
            return string.Empty;
        }
        string stringValue = string.Empty;
        if (cell.CellType == CellType.String)
        {
            stringValue = cell.StringValue;
        }
        if (cell.CellType == CellType.Boolean)
        {
            stringValue = cell.BooleanValue + string.Empty;
        }
        if (cell.CellType == CellType.Blank)
        {
            stringValue = string.Empty;
        }
        if (cell.CellType == CellType.Numeric)
        {
            stringValue = cell.NumberValue + string.Empty;
        }
        stringValue = stringValue.Replace("\"", "\"\"");
        if (stringValue.Contains(","))
        {
            stringValue = "\"" + stringValue + "\"";
        }
        return stringValue;
    }

    private static string ToString(ICell cell)
    {
        if (cell == null)
        {
            return string.Empty;
        }
        string stringCellValue = string.Empty;
        if (cell.CellType == CellType.String)
        {
            stringCellValue = cell.StringCellValue;
        }
        if (cell.CellType == CellType.Boolean)
        {
            stringCellValue = cell.BooleanCellValue + string.Empty;
        }
        if (cell.CellType == CellType.Blank)
        {
            stringCellValue = string.Empty;
        }
        if (cell.CellType == CellType.Numeric)
        {
            stringCellValue = cell.NumericCellValue + string.Empty;
        }
        if ((cell.CellType == CellType.Formula) && cell.CellFormula.Contains("!"))
        {
            return ToString(cell.Sheet.Workbook.GetCreationHelper().CreateFormulaEvaluator().Evaluate(cell));
        }
        stringCellValue = stringCellValue.Replace("\"", "\"\"");
        if (stringCellValue.Contains(","))
        {
            stringCellValue = "\"" + stringCellValue + "\"";
        }
        return stringCellValue;
    }
}

